/**
 * 
 */
package leetCode;

/**
 * @author zhongfang
 *
 */
public class MaximumLengthofRepeatedSubarray {
	public int findLength(int[] A, int[] B) {
		int n = A.length, m = B.length;
		if (n == 0 || m == 0) {
			return 0;
		}
		int[][] dp = new int[n][m];
		int max = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (A[i] == B[j]) {
					dp[i][j] = ((i != 0 && j != 0) ? dp[i - 1][j - 1] : 0) + 1;
					max = Math.max(max, dp[i][j]);
				}
			}
		}
		return max;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] A = { 1, 2, 3, 2, 1 };
		int[] B = { 3, 2, 1, 4, 7 };
		System.out.println(new MaximumLengthofRepeatedSubarray().findLength(A, B));
	}

}
